from fastapi import FastAPI
from threading import Lock

app = FastAPI()

# 创建一个锁对象，用于同步对全局变量的访问
lock = Lock()

# 全局变量，用于存储调用次数
call_count = 0


@app.get("/")
async def root():
    return {"message": "Hello World"}


@app.get("/hello/{name}")
async def say_hello(name: str):
    global call_count

    # 获取锁，确保线程安全
    with lock:
        call_count += 1
        print(f"Hello {name}, call {call_count}")

    return {"message": f"Hello {name}"}
